package com.michael.self.demompchart

import android.graphics.Color
import android.graphics.Matrix
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.XAxis.XAxisPosition
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
import com.github.mikephil.charting.utils.MPPointF
import java.util.Objects

class BarChartActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bar_chart)
        val mBarChart = findViewById<BarChart>(R.id.mBarChart)
        initChart(mBarChart)
    }

    private fun initChart(mBarChart: BarChart) {
        mBarChart.apply {
            val mMyMarkerView=MyMarkerView(this@BarChartActivity,R.layout.layout_markview)
            marker=mMyMarkerView
            setOnChartValueSelectedListener(object :OnChartValueSelectedListener{
                override fun onValueSelected(e: Entry?, h: Highlight?) {

                    if (h==null) return
                    mMyMarkerView.offset= MPPointF().apply {
                        //判断线到上下左右的距离是否容得下markview
                        if (mBarChart.width-h.xPx<mMyMarkerView.width){
                            //左移

                            x=- (mMyMarkerView.width / 2).toFloat()*2
                        }else if (h.xPx-mBarChart.width<mMyMarkerView.width){
                            //右移
                            x=0f
                            Log.d("TAG", "onValueSelected:右移 ")
                        }


                    }
                    Log.d("TAG", "onValueSelected h.xPx:${h?.xPx} ")
                    Log.d("TAG", "onValueSelected mBarChart.width:${mBarChart.width} ")
                }

                override fun onNothingSelected() {

                }

            })


            setDrawBarShadow(false)
            setDrawGridBackground(false)
            axisRight.isEnabled = false
            description.isEnabled = false
            //折线图例 标签设置
            legend.form = Legend.LegendForm.CIRCLE
            legend.isEnabled = false
            setPinchZoom(true)
            isDragEnabled = true
//            setTouchEnabled(false)
            isScaleXEnabled = false
            isScaleYEnabled = false

            xAxis.position = XAxisPosition.BOTTOM
            val list1 = mutableListOf<BarEntry>()
            for (i in 0..7) {
                list1.add(BarEntry(i.toFloat(), i + 10.toFloat()))
            }
            val list2 = mutableListOf<BarEntry>()
            for (i in 0..7) {
                list2.add(BarEntry(i.toFloat(), i + 15.toFloat()))
            }
            val mBarDataSet1 = BarDataSet(list1, "").apply {
                color = Color.RED
            }
            val mBarDataSet2 = BarDataSet(list2, "").apply {
                color = Color.BLUE
            }
            val mBarData = BarData(mBarDataSet1, mBarDataSet2)
            data = mBarData
            data.setDrawValues(false)
            //能够滑动的关键在此
            val ratio = list1.size / 7f
            zoom(ratio, 0f, 0f, 0f)

            val groupSpace = 0.55f
            val barSpace = 0.02f
            mBarChart.xAxis.axisMinimum = 0f
            xAxis.axisMaximum = barData.getGroupWidth(groupSpace, barSpace) * list1.size
            groupBars(0f, groupSpace, barSpace)
            barData.barWidth = 0.5f
            invalidate()


        }
    }
}